package com.way.floatwindow;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;

import com.way.gesture.R;

public class ATouchFloatView extends ImageView {
	private final Callbacks mCallbacks;
	private WindowManager mWindowManager;
	private WindowManager.LayoutParams mLayoutParams;
	/**
	 * 记录当前手指位置在屏幕上的横坐标值
	 */
	private float xInScreen;

	/**
	 * 记录当前手指位置在屏幕上的纵坐标值
	 */
	private float yInScreen;

	/**
	 * 记录手指按下时在屏幕上的横坐标的值
	 */
	private float xDownInScreen;

	/**
	 * 记录手指按下时在屏幕上的纵坐标的值
	 */
	private float yDownInScreen;

	/**
	 * 记录手指按下时在小悬浮窗的View上的横坐标的值
	 */
	private float xInView;

	/**
	 * 记录手指按下时在小悬浮窗的View上的纵坐标的值
	 */
	private float yInView;

	public ATouchFloatView(Context context, Callbacks callbacks) {
		super(context);
		mCallbacks = callbacks;
		mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);

		int screenWidth = getDisplayWidth(context);
		int screenHeight = getDisplayHeight(context);
		mLayoutParams = new WindowManager.LayoutParams();
		mLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
		mLayoutParams.format = PixelFormat.RGBA_8888;
		mLayoutParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
		mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
		mLayoutParams.width = mLayoutParams.height =
				WindowManager.LayoutParams.WRAP_CONTENT;
				//getResources().getDimensionPixelOffset(R.dimen.atouch_view_width);
		mLayoutParams.x = screenWidth;
		mLayoutParams.y = screenHeight / 2;
		setImageResource(R.drawable.btn_assistivetouch);
		setAlpha(0.4f);
		mWindowManager.addView(this, mLayoutParams);

	}
	@Override
	protected void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
			Log.i("way", "configuration changed: "
					+ getResources().getConfiguration());
		updateViewFinalPosition();
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
			setImageResource(R.drawable.btn_assistivetouch_pressed);
			setAlpha(1.0f);
			xInView = event.getX();
			yInView = event.getY();
			xDownInScreen = event.getRawX();
			yDownInScreen = event.getRawY() - getStatusBarHeight();
			xInScreen = event.getRawX();
			yInScreen = event.getRawY() - getStatusBarHeight();
			break;
		case MotionEvent.ACTION_MOVE:
			xInScreen = event.getRawX();
			yInScreen = event.getRawY() - getStatusBarHeight();
			// 手指移动的时候更新小悬浮窗的位置
			updateViewPosition();
			break;
		case MotionEvent.ACTION_UP:
			setImageResource(R.drawable.btn_assistivetouch);
			setAlpha(0.4f);
			// 如果手指离开屏幕时，xDownInScreen和xInScreen相等，且yDownInScreen和yInScreen相等，则视为触发了单击事件。
			if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) {
				mCallbacks.onClick();
			}
			updateViewFinalPosition();
			break;
		default:
			break;
		}
		return false;
	}

	private void updateViewFinalPosition() {
		final int width = getDisplayWidth(getContext());
		if (mLayoutParams.x > width / 2) {
			mLayoutParams.x = width;
		} else {
			mLayoutParams.x = 0;
		}
		mWindowManager.updateViewLayout(this, mLayoutParams);
	}
	public void removeFromWindow() {
		if (mWindowManager != null) {
			mWindowManager.removeView(this);
		}
	}
	/**
	 * 用于获取状态栏的高度。
	 * 
	 * @return 返回状态栏高度的像素值。
	 */
	private int getStatusBarHeight() {
		return getResources().getDimensionPixelOffset(R.dimen.status_bar_height);
	}

	/**
	 * 更新小悬浮窗在屏幕中的位置。
	 */
	private void updateViewPosition() {
		mLayoutParams.x = (int) (xInScreen - xInView);
		mLayoutParams.y = (int) (yInScreen - yInView);
		mWindowManager.updateViewLayout(this, mLayoutParams);
	}

	/**
	 * 获取手机屏幕高度
	 * 
	 * @param context
	 * @return
	 */
	public int getDisplayHeight(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		mWindowManager.getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	/**
	 * 获取手机屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public int getDisplayWidth(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		mWindowManager.getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public interface Callbacks {
		public void onClick();
	}
}
